/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.playdialog;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.MainFrame;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.GameManagerMenu;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.LinkedList;
import java.util.List;

import javax.jws.Oneway;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;

/**
 * @author Massimo Canonaco
 *
 */
public class PrePlayMatchDialog 
{
	/** */
	private JDialog dialog;
	
	/** */
	private CardLayout cardLayout;
	
	private JPanel container;
	
	/** */
	private MainFrame frameOwner;
	
	/** */
	private OnLinePPM onLinePPM;
	
	public PrePlayMatchDialog(final GameManagerMenu gameManagerMenu)
	{
		MainFrame frame = gameManagerMenu.getMainFrame();
		frameOwner = frame;
		PPMPanel panel = new PPMPanel(gameManagerMenu, this);
//		panel.setMinimumSize(new Dimension(400,310));
		container = new JPanel();
		cardLayout = new CardLayout();
//		cardLayout.addLayoutComponent(panel, PPMPanel.NAME);
		container.setLayout(cardLayout);
		container.add(panel, PPMPanel.NAME);
		dialog = new JDialog(frame,true);
		dialog.setUndecorated(true);
		dialog.add(container);
//		dialog.add(panel);
		dialog.setMinimumSize(panel.getMinimumSize());
		dialog.setSize(panel.getSize());
		dialog.setPreferredSize(panel.getPreferredSize());
//		dialog.setLocation((frame.getWidth()/4),(frame.getHeight()/4));
		dialog.setLocation((frame.getWidth()-panel.getWidth())/4,(frame.getHeight()-panel.getHeight())/4);
		onLinePPM = new OnLinePPM(this);
		container.add(onLinePPM, OnLinePPM.NAME);
//		dialog.setLocation((frame.getHeight()-panel.getHeight())/2,(frame.getWidth()-panel.getWidth())/2);
	}
	
	public void show()
	{
		dialog.setVisible(false);
		cardLayout.show(container, PPMPanel.NAME);
		dialog.setVisible(true);
	}
	
	public void close()
	{
		dialog.setVisible(false);
//		dialog.dispose();
	}
	
	public void hide()
	{
		dialog.setVisible(false);
	}
	/** */
	public void showOnLinePPM()
	{
		dialog.setVisible(false);
		onLinePPM.getConnectionThread().start();
		cardLayout.show(container, OnLinePPM.NAME);
		dialog.setVisible(true);
//		dialog.set
	}
	
	/** */
	private class PPMPanel extends JPanel
	{
		/** */
		static private final String NAME = "ppmpanelbase";
		private PPMPanel(final GameManagerMenu gameManagerMenu, final PrePlayMatchDialog dialog)
		{
			super();
			final float TEXT_SIZE = 40.0f;
			Dimension dimension = new Dimension(150, 90);
			OverallGUILayoutFactory factory = OverallGUILayoutFactory.getInstance();
			JButton singlePlayer = factory.createButtonUniState(TEXT_SIZE, "Locale", dimension.width, dimension.height, OverallGUILayoutFactory.BUTTON_STATE_ACTIVE);
			singlePlayer.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) 
				{
					gameManagerMenu.showSMP();
					dialog.close();
				}
			});
			
			JButton backButton = factory.createButtonUniState(TEXT_SIZE, "Annulla", dimension.width, dimension.height, OverallGUILayoutFactory.BUTTON_STATE_ACTIVE);
			backButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) 
				{
					dialog.close();
				}
			});
			
			JButton onLine = factory.createButtonUniState(TEXT_SIZE, "Online", dimension.width, dimension.height, OverallGUILayoutFactory.BUTTON_STATE_ACTIVE);
			onLine.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					dialog.showOnLinePPM();
				}
			});
			
			add(singlePlayer);
			add(onLine);
			add(backButton);
		}
	}

	/**
	 * @return the frameOwner
	 */
	public MainFrame getFrameOwner() {
		return frameOwner;
	}

	/**
	 * @return the dialog
	 */
	public JDialog getDialog() {
		return dialog;
	}
	
}
